<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>还书请求</title>
    <link rel="stylesheet" href="css/font-awesome.min.css" >
    <style>
        html{
            background-image: url("img/messageback.jpg");
            background-repeat: no-repeat;
            background-size: cover;
        }
        .card{
            display: flex;
            justify-content: space-between;
            margin-left: 10px;
            margin-bottom: 15px;
            padding: 24px 18px;
            background-color: white;
            -webkit-box-shadow: 0 2px 4px 0 rgb(121 146 185);
            box-shadow: 0 2px 4px 0 rgb(121 146 185);
            border-radius: 4px;
            overflow: hidden;
            text-overflow: ellipsis;
            word-break: break-word;
            -webkit-box-orient: vertical;
            font-size: 16px;
            line-height: 18px;

        }
        .card .from{
            margin-bottom: 10px;
        }
        .card .time{
            margin-top: 10px;
            font-size: 10px;
            color: gray;
        }
        .button {
            border-radius: 5px;
            background-color: #4CAF50; /* Green */
            border: none;
            color: white;
            padding: 8px 16px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            -webkit-transition-duration: 0.4s; /* Safari */
            transition-duration: 0.4s;
            cursor: pointer;
        }
        .button2 {
            background-color: white;
            color: black;
            border: 2px solid #008CBA;
        }

        .button2:hover {
            background-color: #008CBA;
            color: white;
        }

        .button3 {
            background-color: white;
            color: black;
            border: 2px solid #f44336;
        }

        .button3:hover {
            background-color: #f44336;
            color: white;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="head" style="margin-top:10px;background-image: linear-gradient(to right, #6a11cb 0%, #2575fc 100%);color: white;font-size: large;margin-bottom: 10px">
        目前共有 {{list_len}} 条申请
    </div>
    <div class="card" v-for="item in requests.list" :key="item.time" style="font-family: Arial, Helvetica, sans-serif;">
        <div class="left" style="float:left;">
            <div class="from">
                用户 <b>{{item.u_name}}</b> 请求还书 <br>
                书名：{{item.b_name}}
            </div>
            <div class="time" style="font-family:Times New Roman, Times, serif;">{{item.time}}</div>
        </div>
        <div class="right" style="float:right">
            <button class="button button2" @click="confirm(item)">同意</button>
            <button class="button button3" @click="refuse(item)">拒绝</button>
        </div>
    </div>

</div>
<script src="js/jquery-3.6.0.slim.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
<script src="js/vue.global.js"></script>
<script src="js/axios.min.js"></script>
<script src="js/my-axios.js"></script>
<script src="layer/layer.js"></script>

<script>
    const app = {
        data() {
            return{
                requests:{},
                list_len:0,
                requestInfo:{},
                refuseInfo:{},
            };
        },

        methods: {
            showMessage(){
                const loadIdx = layer.load(0, {
                    success: () => {//等待加载提示显示出来后，再加载数据，这样可以避免加载提示一直在转圈圈。
                        //加载数据
                        myAxios.get('/showRequest').then((response) => {
                            this.requests = response.data.data;
                            console.log(this.requests)
                            this.list_len = this.requests.list.length
                            layer.close(loadIdx);
                        }).catch(err => {
                            layer.close(loadIdx);
                        }).finally(() => {
                            layer.close(loadIdx);
                        });
                    }
                });
            },

            confirm(item){
                this.requestInfo.u_id = item.u_id
                this.requestInfo.b_id = item.b_id
                const loadIdx = layer.load(0, {
                    success: () => {
                        myAxios.get("/confirmRequest",{params:this.requestInfo}).then((response) => {
                            layer.msg("还书成功！",{icon: 1});
                            layer.close(loadIdx);
                            layer.close(this.messageIdx);
                            this.showMessage()
                        }).catch(err => {
                            layer.msg("同意失败",{icon: 2});
                            layer.close(loadIdx);
                        }).finally(() => {
                            layer.close(loadIdx);
                        });
                    }
                });
            },

            refuse(item){
                this.refuseInfo.from_id = item.u_id
                this.refuseInfo.b_id = item.b_id
                const loadIdx = layer.load(0, {
                    success: () => {
                        myAxios.get("/refuseRequest",{params:this.refuseInfo}).then((response) => {
                            layer.msg("拒绝成功！",{icon: 1});
                            layer.close(loadIdx);
                            layer.close(this.messageIdx);
                            this.showMessage()
                        }).catch(err => {
                            layer.msg("拒绝失败",{icon: 2});
                            layer.close(loadIdx);
                        }).finally(() => {
                            layer.close(loadIdx);
                        });
                    }
                });
            }

        },

        mounted() {
            this.showMessage();
        }

    };

    Vue.createApp(app).mount('#app')

</script>
</body>
</html>